package web;

import service.impl.OrderServiceImpl;
import ssm.pojo.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/order")
public class OrderServlet extends BeanUser {

    /**
     * 添加订单
     *
     * @param request  请求
     * @param response 响应
     */

    public void addOrder(HttpServletRequest request, HttpServletResponse response) {
        try {
            //用户
            User user = (User) request.getSession().getAttribute("user");
            if (user == null) {
                request.setAttribute("msg", "抱歉,您还没有登录,请<a href='http://localhost:8080/11_22_war/jsp/login.jsp'>登录</a>");
                request.getRequestDispatcher("/jsp/jihuo.jsp").forward(request, response);
            }
            //购物车
            Cart cart = (Cart) request.getSession().getAttribute("cart");
            //传入相关参数 获取订单
            OrderServiceImpl orderService = new OrderServiceImpl();
            Orders orders = orderService.addOrder(user, cart);
            if (orders != null) {
                cart.cleanCartItem();
                request.setAttribute("orders", orders);
                request.getRequestDispatcher("/jsp/order_info.jsp").forward(request, response);
            } else {
                request.setAttribute("msg", "抱歉,创建订单失败,请重新创建订单");
                request.getRequestDispatcher("/jsp/jihuo.jsp").forward(request, response);
            }

        } catch (Exception e) {
            try {
                String message = e.getMessage();
                if (message.equals("null")) {
                    request.setAttribute("msg", "抱歉,创建订单失败,请重新创建订单");
                    request.getRequestDispatcher("/jsp/jihuo.jsp").forward(request, response);
                }
            } catch (ServletException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 通过用户查询订单分页
     *
     * @param request  请求
     * @param response 响应
     */
    public void findOrdersgrPage(HttpServletRequest request, HttpServletResponse response) {

        try {
            User user = (User) request.getSession().getAttribute("user");
            //当前页数
            String num = request.getParameter("number");
            //每页显示条数
            String tot = request.getParameter("total");
            if (num == null || num.equals("")) {
                num = "1";
            }
            if (tot == null || tot.equals("")) {
                tot = "4";
            }
            int number = Integer.parseInt(num);
            int total = Integer.parseInt(tot);
            OrderServiceImpl orderService = new OrderServiceImpl();
            PageBean<Orders> ordersPage = orderService.orderByPage(user, number, total);
            if(ordersPage!=null){
                request.setAttribute("ordersPage",ordersPage);
                request.getRequestDispatcher("/jsp/order_list.jsp").forward(request,response);
            }else {
                request.setAttribute("msg","暂时没有订单,请购物吧");
                request.getRequestDispatcher("/jsp/jihuo.jsp").forward(request,response);
            }
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
